const Base = require('./base.js');
const fs = require('fs');
const xlsx = require("node-xlsx");

module.exports = class extends Base {
  async avatarAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/avatar/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename
    });
  }

  async bannerAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/banner/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename
    });
  }

  async newsThumbAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/news/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename
    });
  }

  async figureUploadAction(){

    const file = this.file('file');
    let xlsxData  = xlsx.parse(file.path);
    
    const arr = xlsxData[0].data
    const newArr = []
    arr.shift()
    console.log(arr,888)
    console.log(arr[0].length)
    if(think.isEmpty(arr)){
      this.fail(408, 'excel文件出错')
    }

    let insertData = []

    for (const item of arr) {
      let has_contact = 0
      if(item[7] || item[8] ){
        has_contact = 1
      }
      if(item[0]){
        const originData = {
          name: item[0],
          company_name: item[1],
          position_name: item[2],
          identity: item[3],
          education: item[4],
          industry: item[5],
          round: item[6],
          phone: item[7],
          wechat: item[8],
          mail: item[9],
          info: item[10],
          company_info: item[11],
          message: item[12],
          tag: item[13],
          create_time: ['exp', 'CURRENT_TIMESTAMP()'],
          has_contact: has_contact
        }
        insertData.push(originData)
      }
      // names.push(item[0])
    }
    // await this.model('figure').where({name: ['IN', names]}).delete();
    await this.model('figure').addMany(insertData)

    return this.success();
  }

  async checkVipAction(){
    await this.model('user').where('DATEDIFF(curdate(),end_time)>0').update({
      is_vip: 0
    })

    await this.model('figure').where('DATEDIFF(curdate(),dated_time)>0').update({
      is_vip: 0
    })

    return this.success();
  }
};
